package com.example.dmallOrder.common.exception;

import com.example.dmallOrder.common.result.IResultEnum;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.io.Serial;

/**
 * 异常类
 */
@Slf4j
@Getter
@ToString
public class CommonException extends RuntimeException {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 错误信息
     */
    private String msg;


    public CommonException() {
        super();
    }

    public CommonException(String msg) {
        super(msg);
        this.msg = msg;
    }

    public CommonException(Integer code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public CommonException(IResultEnum iResultEnum) {
        super(iResultEnum.getMsg());
        this.code = iResultEnum.getCode();
        this.msg = iResultEnum.getMsg();
    }

    public CommonException(IResultEnum iResultEnum, Throwable throwable) {
        super(iResultEnum.getMsg() + ", " + throwable.getMessage(), throwable);
        this.code = iResultEnum.getCode();
        this.msg = iResultEnum.getMsg();
    }

}
